﻿/**
 * @file action.h
 * @brief this is a brief description
 * @details this is the detail description.
 * @author fulong 
 * @data 9/2/2014
 * @version 1.0
 * @par Copyright (c):
 * @par First build
 *     version: fulong 
 *     data: 9/2/2014
 */
#ifndef __ACTION_H__
#define __ACTION_H__
#include "../../hal_include/hal_types.h"

typedef union {
    struct {
        uint8 bit0:1;
        uint8 bit1:1;
        uint8 bit2:1;
        uint8 bit3:1;
        uint8 bit4:1;
        uint8 bit5:1;
        uint8 bit6:1;
        uint8 bit7:1;
    }bit;
    uint8 M_byte;
}modbus_inner_var_struct;

typedef union {
    struct {
        uint8 bit0:1;
        uint8 bit1:1;
        uint8 bit2:1;
        uint8 bit3:1;
        uint8 bit4:1;
        uint8 bit5:1;
        uint8 bit6:1;
        uint8 bit7:1;
        uint8 bit10:1;
        uint8 bit11:1;
        uint8 bit12:1;
        uint8 bit13:1;
        uint8 bit14:1;
        uint8 bit15:1;
        uint8 bit16:1;
        uint8 bit17:1;
        uint8 bit20:1;
        uint8 bit21:1;
        uint8 bit22:1;
        uint8 bit23:1;
        uint8 bit24:1;
        uint8 bit25:1;
        uint8 bit26:1;
        uint8 bit27:1;
        uint8 bit30:1;
        uint8 bit31:1;
    }action_output_bit;
    uint32 action_output_byte;
}action_output;
typedef enum {
    MANUAL_ACTION,
    AUTO_ACTION,
    NO_ACTION,
    ACTION
}action_stuct;

#define ACTION_ENABLE 0
#define ACTION_DISABLE (!ACTION_ENABLE)

#define PARAM_NUM_MAX (102)
#define PARAM_MAX (2*PARAM_NUM_MAX)

#define M00 modbus_inner_var[0].bit.bit0
#define M01 modbus_inner_var[0].bit.bit1
#define M02 modbus_inner_var[0].bit.bit2
#define M03 modbus_inner_var[0].bit.bit3
#define M04 modbus_inner_var[0].bit.bit4
#define M05 modbus_inner_var[0].bit.bit5
#define M06 modbus_inner_var[0].bit.bit6
#define M07 modbus_inner_var[0].bit.bit7

#if OUTPUT_MAX_NUM > 0
#define   Y00 action_output_ctrl.action_output_bit.bit0
#endif
#if OUTPUT_MAX_NUM > 1
#define   Y01 action_output_ctrl.action_output_bit.bit1
#endif
#if OUTPUT_MAX_NUM > 2
#define   Y02 action_output_ctrl.action_output_bit.bit2
#endif
#if OUTPUT_MAX_NUM > 3
#define   Y03 action_output_ctrl.action_output_bit.bit3
#endif
#if OUTPUT_MAX_NUM > 4
#define   Y04 action_output_ctrl.action_output_bit.bit4
#endif
#if OUTPUT_MAX_NUM > 5
#define   Y05 action_output_ctrl.action_output_bit.bit5
#endif
#if OUTPUT_MAX_NUM > 6
#define   Y06 action_output_ctrl.action_output_bit.bit6
#endif
#if OUTPUT_MAX_NUM > 7
#define   Y07 action_output_ctrl.action_output_bit.bit7
#endif
#if OUTPUT_MAX_NUM > 8
#define   Y10 action_output_ctrl.action_output_bit.bit10
#endif
#if OUTPUT_MAX_NUM > 9
#define   Y11 action_output_ctrl.action_output_bit.bit11
#endif
#if OUTPUT_MAX_NUM > 10
#define   Y12 action_output_ctrl.action_output_bit.bit12
#endif
#if OUTPUT_MAX_NUM > 11
#define   Y13 action_output_ctrl.action_output_bit.bit13
#endif
#if OUTPUT_MAX_NUM > 12
#define   Y14 action_output_ctrl.action_output_bit.bit14
#endif
#if OUTPUT_MAX_NUM > 13
#define   Y15 action_output_ctrl.action_output_bit.bit15
#endif
#if OUTPUT_MAX_NUM > 14
#define   Y16 action_output_ctrl.action_output_bit.bit16
#endif
#if OUTPUT_MAX_NUM > 15
#define   Y17 action_output_ctrl.action_output_bit.bit17
#endif
#if OUTPUT_MAX_NUM > 16
#define   Y20 action_output_ctrl.action_output_bit.bit20
#endif
#if OUTPUT_MAX_NUM > 17
#define   Y21 action_output_ctrl.action_output_bit.bit21
#endif

#if INPUT_MAX_NUM > 0
#define X00 !status_process.input_cur.bit.in0
#endif
#if INPUT_MAX_NUM > 1
#define X01 !status_process.input_cur.bit.in1
#endif
#if INPUT_MAX_NUM > 2
#define X02 !status_process.input_cur.bit.in2
#endif
#if INPUT_MAX_NUM > 3
#define X03 !status_process.input_cur.bit.in3
#endif
#if INPUT_MAX_NUM > 4
#define X04 !status_process.input_cur.bit.in4
#endif
#if INPUT_MAX_NUM > 5
#define X05 !status_process.input_cur.bit.in5
#endif
#if INPUT_MAX_NUM > 6
#define X06 !status_process.input_cur.bit.in6
#endif
#if INPUT_MAX_NUM > 7
#define X07 !status_process.input_cur.bit.in7
#endif
#if INPUT_MAX_NUM > 8
#define X10 !status_process.input_cur.bit.in10
#endif
#if INPUT_MAX_NUM > 9
#define X11 !status_process.input_cur.bit.in11
#endif
#if INPUT_MAX_NUM > 10
#define X12 !status_process.input_cur.bit.in12
#endif
#if INPUT_MAX_NUM > 11
#define X13 !status_process.input_cur.bit.in13
#endif
#if INPUT_MAX_NUM > 12
#define X14 !status_process.input_cur.bit.in14
#endif
#if INPUT_MAX_NUM > 13
#define X15 !status_process.input_cur.bit.in15
#endif
#if INPUT_MAX_NUM > 14
#define X16 !status_process.input_cur.bit.in16
#endif
#if INPUT_MAX_NUM > 15
#define X17 !status_process.input_cur.bit.in17
#endif
#if INPUT_MAX_NUM > 16
#define X20 !status_process.input_cur.bit.in20
#endif
#if INPUT_MAX_NUM > 17
#define X21 !status_process.input_cur.bit.in21
#endif
#if INPUT_MAX_NUM > 18
#define X22 !status_process.input_cur.bit.in22
#endif
#if INPUT_MAX_NUM > 19
#define X23 !status_process.input_cur.bit.in23
#endif
#if INPUT_MAX_NUM > 20
#define X24 !status_process.input_cur.bit.in24
#endif
#if INPUT_MAX_NUM > 21
#define X25 !status_process.input_cur.bit.in25
#endif


#if INPUT_MAX_NUM > 0
#define X00_OLD !status_process.input_old.bit.in0
#define bOldX00 X00_OLD
#define X00_UP (X00 && !bOldX00)
#define X00_DW (!X00 && bOldX00)
#endif
#if INPUT_MAX_NUM > 1
#define X01_OLD  !status_process.input_old.bit.in1
#define bOldX01 X01_OLD
#define X01_UP (X01 && !bOldX01)
#define X01_DW (!X01 && bOldX01)
#endif
#if INPUT_MAX_NUM > 2
#define X02_OLD  !status_process.input_old.bit.in2
#define bOldX02 X02_OLD
#define X02_UP (X02 && !bOldX02)
#define X02_DW (!X02 && bOldX02)
#endif
#if INPUT_MAX_NUM > 3
#define X03_OLD  !status_process.input_old.bit.in3
#define bOldX03 X03_OLD
#define X03_UP (X03 && !bOldX03)
#define X03_DW (!X03 && bOldX03)
#endif
#if INPUT_MAX_NUM > 4
#define X04_OLD  !status_process.input_old.bit.in4
#define bOldX04 X04_OLD
#define X04_UP (X04 && !bOldX04)
#define X04_DW (!X04 && bOldX04)
#endif
#if INPUT_MAX_NUM > 5
#define X05_OLD  !status_process.input_old.bit.in5
#define bOldX05 X05_OLD
#define X05_UP (X05 && !bOldX05)
#define X05_DW (!X05 && bOldX05)
#endif
#if INPUT_MAX_NUM > 6
#define X06_OLD  !status_process.input_old.bit.in6
#define bOldX06 X06_OLD
#define X06_UP (X06 && !bOldX06)
#define X06_DW (!X06 && bOldX06)
#endif
#if INPUT_MAX_NUM > 7
#define X07_OLD  !status_process.input_old.bit.in7
#define bOldX07 X07_OLD
#define X07_UP (X07 && !bOldX07)
#define X07_DW (!X07 && bOldX07)
#endif
#if INPUT_MAX_NUM > 8
#define X10_OLD  !status_process.input_old.bit.in10
#define bOldX10 X10_OLD
#define X10_UP (X10 && !bOldX10)
#define X10_DW (!X10 && bOldX10)
#endif
#if INPUT_MAX_NUM > 9
#define X11_OLD  !status_process.input_old.bit.in11
#define bOldX11 X11_OLD
#define X11_UP (X11 && !bOldX11)
#define X11_DW (!X11 && bOldX11)
#endif
#if INPUT_MAX_NUM > 10
#define X12_OLD  !status_process.input_old.bit.in12
#define bOldX12 X12_OLD
#define X12_UP (X12 && !bOldX12)
#define X12_DW (!X12 && bOldX12)
#endif
#if INPUT_MAX_NUM > 11
#define X13_OLD  !status_process.input_old.bit.in13
#define bOldX13 X13_OLD
#define X13_UP (X13 && !bOldX13)
#define X13_DW (!X13 && bOldX13)
#endif
#if INPUT_MAX_NUM > 12
#define X14_OLD  !status_process.input_old.bit.in14
#define bOldX14 X14_OLD
#define X14_UP (X14 && !bOldX14)
#define X14_DW (!X14 && bOldX14)
#endif
#if INPUT_MAX_NUM > 13
#define X15_OLD  !status_process.input_old.bit.in15
#define bOldX15 X15_OLD
#define X15_UP (X15 && !bOldX15)
#define X15_DW (!X15 && bOldX15)
#endif
#if INPUT_MAX_NUM > 14
#define X16_OLD  !status_process.input_old.bit.in16
#define bOldX16 X16_OLD
#define X16_UP (X16 && !bOldX16)
#define X16_DW (!X16 && bOldX16)
#endif
#if INPUT_MAX_NUM > 15
#define X17_OLD  !status_process.input_old.bit.in17
#define bOldX17 X17_OLD
#define X17_UP (X17 && !bOldX17)
#define X17_DW (!X17 && bOldX17)
#endif
#if INPUT_MAX_NUM > 16
#define X20_OLD  !status_process.input_old.bit.in20
#define bOldX20 X20_OLD
#define X20_UP (X20 && !bOldX20)
#define X20_DW (!X20 && bOldX20)
#endif
#if INPUT_MAX_NUM > 17
#define X21_OLD  !status_process.input_old.bit.in21
#define bOldX21 X21_OLD
#define X21_UP (X21 && !bOldX21)
#define X21_DW (!X21 && bOldX21)
#endif
#if INPUT_MAX_NUM > 18
#define X22_OLD  !status_process.input_old.bit.in22
#define bOldX22 X22_OLD
#define X22_UP (X22 && !bOldX22)
#define X22_DW (!X22 && bOldX22)
#endif
#if INPUT_MAX_NUM > 19
#define X23_OLD  !status_process.input_old.bit.in23
#define bOldX23 X23_OLD
#define X23_UP (X23 && !bOldX23)
#define X23_DW (!X23 && bOldX23)
#endif
#if INPUT_MAX_NUM > 20
#define X24_OLD  !status_process.input_old.bit.in24
#define bOldX24 X24_OLD
#define X24_UP (X24 && !bOldX24)
#define X24_DW (!X24 && bOldX24)
#endif
#if INPUT_MAX_NUM > 21
#define X25_OLD  !status_process.input_old.bit.in25
#define bOldX25 X25_OLD
#define X25_UP (X25 && !bOldX25)
#define X25_DW (!X25 && bOldX25)
#endif

#define set_param(i,data) {param[(i)*2] = (data >>8);param[(i)*2+1] = data;e2prom_write_single_byte((i)* 2, (data)>> 8); e2prom_write_single_byte((i)* 2 + 1, (data));}
#define read_param_h8(i) (param[(i)*2])
#define read_param_l8(i) (param[(i)*2+1])
//....总共16个参数
#define read_action_param(i) (((uint16)param[(i)*2] << 8) + param[(i)*2+1])

extern action_output action_output_ctrl;
extern modbus_inner_var_struct modbus_inner_var[ 25 ];
extern uint8 param[ PARAM_MAX ];

extern void mechine_action(void);
extern void mechine_action_init(void);

#endif /* __ACTION_H__ */


